class Solution {
public:
    int happy(int n)
    {
        int sum = 0;
        while (n > 0)
        {
            int bit = n % 10;
            sum += bit * bit;
            n = n / 10;
        }
        return sum;
    }
    bool isHappy(int n)
    {
        int slow = n;
        int fast = happy(n);
        while (slow != fast)
        {
            slow = happy(slow);
            fast = happy(happy(fast));
        }
        return slow == 1;
    }
};